package com.campus.controller;

import com.campus.model.Student;
import com.campus.service.StudentService;
import com.campus.web.vo.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import static com.campus.common.BusinessConstants.API_BASE;

@Slf4j
@RestController
@RequestMapping( "/students")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @PostMapping("/create")
    public ResponseResult<Student> createStudent(@RequestBody Student student) throws Exception {
        Student student1 = studentService.createStudent(student);
        return ResponseResult.ok(student1,"Student created successfully");
    }

    @PostMapping("/create/batch")
    public ResponseEntity<String> batchCreateStudents(@RequestBody List<Student> students) {
        int result = studentService.batchImportStudents(students);
        return result > 0 ? ResponseEntity.ok("Students created successfully") : ResponseEntity.status(500).body("Failed to create students");
    }

    @PostMapping("/import")
    public ResponseEntity<List<Student>> importStudentsFromExcel(@RequestParam("file") MultipartFile multipartFile) {
        try {
            // 转换 MultipartFile 到 File
            File file = convertMultipartFileToFile(multipartFile);

            List<Student> students = studentService.importStudentsFromExcel(file);
            return ResponseEntity.ok(students);
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        }
    }

    private File convertMultipartFileToFile(MultipartFile multipartFile) throws IOException {
        File file = new File(System.getProperty("java.io.tmpdir") + File.separator + multipartFile.getOriginalFilename());
        InputStream in = multipartFile.getInputStream();
        try (FileOutputStream os = new FileOutputStream(file)) {
            byte[] buffer = new byte[1024 * 1024];
            int length;
            while ((length = in.read(buffer)) != -1) {
                os.write(buffer, 0, length);
            }
        }
        return file;
    }

    @PostMapping("/approve/{id}")
    public ResponseResult<Student> approveStudent(@RequestParam Integer id, @RequestParam Integer status) {
        Student approvedStudent = studentService.approveStudent(id);
        return ResponseResult.ok(approvedStudent,"Student approved successfully");
    }

    @PutMapping("/approve/batch")
    public ResponseEntity<String> batchApproveStudents(@RequestBody List<Integer> studentIds) {
        int result = studentService.batchApproveStudent(studentIds);
        return result > 0 ? ResponseEntity.ok("Students approved successfully") : ResponseEntity.status(500).body("Failed to approve students");
    }
}
